package com.ruoyi.generator.config;

import com.ruoyi.generator.domain.MqttProperty;
import com.ruoyi.generator.util.MQTTClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


@Configuration
@ConditionalOnProperty(value = "mqtt.enable", havingValue = "true")
public class MqttConfig {

    private final Map<String,MQTTClient> map = new ConcurrentHashMap<>();

    public MQTTClient getClient(MqttProperty mqttProperty) {
        if (map.containsKey(mqttProperty.getKey())) {
            return map.get(mqttProperty.getKey());
        }
        MQTTClient client = MQTTClient.getMqttClient(mqttProperty);
        map.put(mqttProperty.getKey(), client);
        return client;
    }

}
